package com.handong.handserialprogram.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.handong.handserialprogram.model.entities.MissionEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/19.
 */

public class DBManager {

    private static final String TAG = "DBManager";
    private static DBManager mDBManager;
    private MissionDB mMissionDB;
    private SQLiteDatabase mWriteDatabase;

    private DBManager(Context context) {
        mMissionDB = new MissionDB(context.getApplicationContext());
    }

    private void initDatabase() {
        if (mWriteDatabase == null) {
            mWriteDatabase = mMissionDB.getWritableDatabase();
            Log.d(TAG, "mSqLiteDatabase is null, init it.");
        }
    }

    public static DBManager getDBManager(Context context) {
        if (mDBManager == null) {
            synchronized (DBManager.class) {
                if (mDBManager == null) {
                    mDBManager = new DBManager(context);
                }
            }
        }
        return mDBManager;
    }

    public void insertMission(List<MissionEntity> list) {
        initDatabase();
        mWriteDatabase.beginTransaction();
        try {
            ContentValues values = null;
            for (MissionEntity entity : list) {
                values = new ContentValues();
                values.put(MissionDB.MISSION_ID, entity.getTaskId());
                values.put(MissionDB.TITLE, entity.getTaskName());
                values.put(MissionDB.START_DATE, entity.getTaskStartDate());
                //values.put(MissionDB.END_DATE, entity.get());
                //values.put(MissionDB.START_POSITION, entity.getTaskBeginPlace());
                //values.put(MissionDB.END_POSITION, entity.getTaskEndPlace());
                values.put(MissionDB.CAR_ID, entity.getTaskEscortCar());
                //values.put(MissionDB.DESCRIPTION, entity.getTaskDescription());
                //values.put(MissionDB.STATUS, entity.getTaskStatue());
                values.put(MissionDB.ROUTE_ID, entity.getTaskPlanningRouteId());
                mWriteDatabase.insert(MissionDB.MISSION_TABLE, null, values);
            }
            mWriteDatabase.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mWriteDatabase.endTransaction();
        }
    }

    public void deleteAllMission() {
        initDatabase();
        mWriteDatabase.beginTransaction();
        try {
            mWriteDatabase.delete(MissionDB.MISSION_TABLE, null, null);
            mWriteDatabase.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mWriteDatabase.endTransaction();
        }
    }

    public List<MissionEntity> queryAllMission() {
        initDatabase();
        List<MissionEntity> list = new ArrayList<>();
        MissionEntity entity = null;
        String sql = "select * from Mission";
        Cursor cursor = mWriteDatabase.rawQuery(sql, null);
        if (!cursor.moveToFirst()) {
            cursor.close();
            cursor = null;
            return null;
        }
        mWriteDatabase.beginTransaction();
        try {
            do {
                entity = new MissionEntity();
                entity.setTaskId(cursor.getString(cursor
                        .getColumnIndex(MissionDB.MISSION_ID)));
                entity.setTaskName(cursor.getString(cursor
                        .getColumnIndex(MissionDB.TITLE)));
                entity.setTaskStartDate(cursor.getString(cursor
                        .getColumnIndex(MissionDB.START_DATE)));
                //entity.setEndDate(cursor.getString(cursor
                        //.getColumnIndex(MissionDB.END_DATE)));
                //entity.setTaskBeginPlace(cursor.getString(cursor
                        //.getColumnIndex(MissionDB.START_POSITION)));
                //entity.setTaskEndPlace(cursor.getString(cursor
                        //.getColumnIndex(MissionDB.END_POSITION)));
                //entity.setTaskEscortCar(cursor.getString(cursor
                        //.getColumnIndex(MissionDB.CAR_ID)));
                //entity.setTaskDescription(cursor.getString(cursor
                        //.getColumnIndex(MissionDB.DESCRIPTION)));
                //entity.setTaskStatue(cursor.getInt(cursor
                       // .getColumnIndex(MissionDB.STATUS)));
                entity.setTaskPlanningRouteId(cursor.getString(cursor
                        .getColumnIndex(MissionDB.ROUTE_ID)));
                list.add(entity);
            } while (cursor.moveToNext());
            mWriteDatabase.setTransactionSuccessful();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            mWriteDatabase.endTransaction();
        }
        return list;
    }

    public void closeDB(){
        if (mWriteDatabase != null){
            mWriteDatabase.close();
            mWriteDatabase = null;
        }

        if (mMissionDB != null){
            mMissionDB.close();
            mMissionDB = null;
        }

        if (mDBManager != null){
            mDBManager = null;
        }
    }
}
